﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QueuesAndStacks.EvalRPN
{
    class MainProject
    {
        static void Main(string[] args)
        {
            Console.WriteLine(EvalRPN(new string[] {"4", "13", "5", "/", "+"}));
            Console.ReadKey();
        }

        /// <summary>
        /// 逆波兰式
        /// </summary>
        /// <param name="tokens"></param>
        /// <returns></returns>
        public static int EvalRPN(string[] tokens)
        {
            Stack<string> sta = new Stack<string>();
            foreach (var item in tokens)
            {
                if (item=="+")
                {
                    int a = int.Parse(sta.Pop());
                    int b = int.Parse(sta.Pop());
                    sta.Push((b + a).ToString());
                }
                else if (item == "-")
                {
                    int a = int.Parse(sta.Pop());
                    int b = int.Parse(sta.Pop());
                    sta.Push((b - a).ToString());
                }
                else if (item == "*")
                {
                    int a = int.Parse(sta.Pop());
                    int b = int.Parse(sta.Pop());
                    sta.Push((b * a).ToString());
                }
                else if (item == "/")
                {
                    int a = int.Parse(sta.Pop());
                    int b = int.Parse(sta.Pop());
                    sta.Push((b / a).ToString());
                }
                else
                {
                    sta.Push(item);
                }
            }
            string str = sta.Pop();
            return int.Parse(str);
        }
    }
}
